document.addEventListener('DOMContentLoaded', function() {
    // 显示/隐藏目标卡号输入框
    const tradeTypeSelect = document.getElementById('tradeType');
    const targetCardGroup = document.getElementById('targetCardGroup');

    if (tradeTypeSelect && targetCardGroup) {
        tradeTypeSelect.addEventListener('change', function() {
            if (this.value === '转账') {
                targetCardGroup.style.display = 'block';
            } else {
                targetCardGroup.style.display = 'none';
            }
        });
    }

    // 添加交易按钮
    const addTradeBtn = document.getElementById('addTradeBtn');
    if (addTradeBtn) {
        addTradeBtn.addEventListener('click', function() {
            // 重置表单
            document.getElementById('tradeType').value = '';
            document.getElementById('cardID').value = '';
            document.getElementById('targetCardID').value = '';
            document.getElementById('tradeMoney').value = '';
            document.getElementById('remark').value = '';
            targetCardGroup.style.display = 'none';

            // 显示模态框
            const modal = new bootstrap.Modal(document.getElementById('tradeModal'));
            modal.show();
        });
    }

    // 删除按钮
    const deleteButtons = document.querySelectorAll('.delete-btn');
    deleteButtons.forEach(button => {
        button.addEventListener('click', function() {
            const tradeID = this.getAttribute('data-id');
            document.getElementById('deleteTradeID').value = tradeID;
            document.getElementById('deleteTradeIDValue').textContent = tradeID;

            const modal = new bootstrap.Modal(document.getElementById('deleteModal'));
            modal.show();
        });
    });

    // 搜索按钮
    const searchBtn = document.getElementById('searchBtn');
    if (searchBtn) {
        searchBtn.addEventListener('click', function() {
            const cardID = document.getElementById('cardIDSearch').value;
            const tradeType = document.getElementById('tradeTypeSearch').value;
            const tradeDate = document.getElementById('tradeDateSearch').value;

            let url = new URL(window.location.href);
            url.searchParams.set('cardIDSearch', cardID);
            url.searchParams.set('tradeTypeSearch', tradeType);
            url.searchParams.set('tradeDateSearch', tradeDate);
            url.searchParams.delete('page'); // 重置到第一页

            window.location.href = url.toString();
        });
    }
});